package com.youlai.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.system.common.model.Option;
import com.youlai.system.converter.SourceConverter;
import com.youlai.system.mapper.SysSourceMapper;
import com.youlai.system.model.entity.SysSource;
import com.youlai.system.model.entity.SysUser;
import com.youlai.system.model.form.SourceForm;
import com.youlai.system.model.query.SourcePageQuery;
import com.youlai.system.model.vo.SourcePageVO;
import com.youlai.system.service.SysSourceService;
import com.youlai.system.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 人员批次业务处理实现
 */
@Service
@RequiredArgsConstructor
public class SysSourceServiceImpl extends ServiceImpl<SysSourceMapper, SysSource> implements SysSourceService {

    private final SourceConverter sourceConverter;

    private final SysUserService userService;

    @Override
    public SysSource getByContentEqual(String content) {
        LambdaQueryWrapper<SysSource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysSource::getContent, content);
        return getOne(queryWrapper);
    }

    @Override
    public Long saveByContent(String content) {
        SysSource source = new SysSource();
        source.setContent(content);
        save(source);
        return source.getId();
    }

    @Override
    public Page<SourcePageVO> getSourcePage(SourcePageQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();

        LambdaQueryWrapper<SysSource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(keywords), SysSource::getContent, keywords);
        //查询数据
        Page<SysSource> sourcePage = this.page(new Page<>(pageNum, pageSize), queryWrapper);
        Map<Long, SysUser> sourceUserCountMap = userService.sourceUserCountMap();
        sourcePage.getRecords().forEach(source -> {
            SysUser user = sourceUserCountMap.get(source.getId());
            if (user != null) {
                source.setUserCount(user.getCount());
                source.setGroupNickName(user.getGroupNickName());
            } else {
                source.setUserCount(0);
                source.setGroupNickName("");
            }
        });
        return sourceConverter.entity2Page(sourcePage);
    }

    @Override
    public boolean saveSource(SourceForm sourceForm) {
        String content = sourceForm.getContent();

        long count = this.count(new LambdaQueryWrapper<SysSource>().eq(SysSource::getContent, content));
        Assert.isTrue(count == 0, "人员批次已存在");

        // 实体转换
        SysSource category = sourceConverter.form2Entity(sourceForm);
        return save(category);
    }

    @Override
    public boolean updateSource(Long sourceId, SourceForm sourceForm) {
        String content = sourceForm.getContent();

        long count = this.count(new LambdaQueryWrapper<SysSource>()
                .eq(SysSource::getContent, content)
                .ne(SysSource::getId, sourceForm.getId())
        );
        Assert.isTrue(count == 0, "人员批次已存在");

        // form -> entity
        SysSource entity = sourceConverter.form2Entity(sourceForm);

        // 修改人员批次
        return this.updateById(entity);
    }

    @Override
    public SourceForm getSourceForm(Long sourceId) {
        SysSource entity = this.getById(sourceId);
        return sourceConverter.entity2Form(entity);
    }

    @Override
    public boolean deleteSources(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "人员批次删除数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    @Override
    public boolean deleteSources(List<Long> idList) {
        return this.removeByIds(idList);
    }

    @Override
    public List<Option> listSourceOptions() {
        // 查询数据
        List<SysSource> sourceList = this.list(new LambdaQueryWrapper<SysSource>()
                .select(SysSource::getId, SysSource::getContent)
                .orderByAsc(SysSource::getSort)
        );

        // 实体转换
        return sourceConverter.entities2Options(sourceList);
    }
}
